/**
 *  Authors:
 *  Scott Renaud
 *  Kevin Carrington
 *  
 *  February 5 - 2012
 * 
 *  Team 4 - Iteration 1
 *  COMP 354 / Winter 2012
 *  
 *  =============================
 *  
 *  IO.java
 *  
 *  Class that handles file reading and output.
 *  Imports from a Task file.
 *  Outputs to a Person file
 *  
 * Note: This class assumes that the file used for input is in the
 *  		    correct format (see Requirements Document)
 *  
 */

package taskManagmentSystem;

//libraries
import java.io.BufferedReader;  
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Vector;
import java.io.PrintWriter;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;

public class IO {

	// Class properties
	private StringBuilder contents = new StringBuilder();
    private StringTokenizer st;
    private Task tempTask;
    private File taskFile ;
    private String personFile ;
    
    // Constructor
    public IO(String inputFile, String outputFile)
    {
    	this.taskFile = new File(inputFile);
    	this.personFile = outputFile;
    }
    
    // Accessors and mutators
    public void setTaskFile(String fileName)
    {
    	this.taskFile = new File(fileName);
    }
    
    public File getTaskFile()
    {
    	return this.taskFile;
    }
    
    public void setPersonFile(String fileName)
    {
    	this.personFile = fileName;
    }
    
    public String getPersonFile()
    {
    	return this.personFile;
    }
    
    // Imports tasks from the input file  to a provided vector of Task objects
	public void processImport(Vector<Task> taskList) 
	{
		  try 
		    {
		      BufferedReader input =  new BufferedReader(new FileReader(taskFile));
		      try {
		        String line = null;
		          
		          System.out.println("");
				  System.out.println("Reading from " + taskFile.getName());
				  System.out.println("");
				  
		        while (( line = input.readLine()) != null)
		        {
		          tempTask = new Task();
		          contents.append(line);
		          
		          //separation of content
		          contents.append(System.getProperty("line.separator"));
		          st = new StringTokenizer(line,",");
	          
		          tempTask.setTaskID(Integer.parseInt((st.nextElement()).toString()));
		          tempTask.setTitle((String)st.nextElement());
		          tempTask.setDescription((String)st.nextElement());
		          tempTask.setDuration(Integer.parseInt((st.nextElement()).toString()));
		          tempTask.setDeliverable((String)st.nextElement());
		          tempTask.setDeadlineDate((String)st.nextElement());
		          tempTask.setPerson((String)st.nextElement());
		          
		          taskList.add(tempTask);	  
		        }       
		        
		      }
		      finally {
		        input.close();
		      }
		    }
		    catch (IOException ex){
		    	System.out.println("\n\n--> The file: " + taskFile.getName() + ", could not be read :( \nPlease try running the program again and entering a valid filename.");
		    	System.exit(0);
		    }
		    
		    for (int i = 0; i < taskList.size(); i++)
		    {
		    	System.out.println(taskList.get(i).toString());
		    }	    
	}
	
	// Exports the Person's info from the provided vector to the the output file
	public void processExport(Vector<Person> personList) 
	{
		  PrintWriter outputStream = null;
		  try
		  {
			  outputStream = new PrintWriter(new FileOutputStream(personFile));
			  //change this path to where the folder "COMP354 - Increment 1" is found
		  }
		  catch(FileNotFoundException e)
		  {
			  System.out.println("There was an ERROR opening the file:  " + taskFile.getName());
			  System.out.println("Please rerun the program and try Again.");
			  System.exit(0);
		  }
		  
		  System.out.println("");
		  System.out.println("Writing to " + personFile);
		  System.out.println("");
		    
		    for (int i = 0; i < personList.size(); i++)
		    {
		    	//outputting person info to screen AND personFile.txt
		    	outputStream.println(personList.get(i).toString());
		    	System.out.println(personList.get(i).toString());
		    }	
		    
		    outputStream.close();
		    
		    System.out.println("");
		    System.out.println("Data successfully written to: " + personFile);
	}
	
	
}
